home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 2106 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  1.6 KB

  1. Path: ocbbs.gen.nz!not-for-mail
  2. From: steve@hn.ocbbs.gen.nz (Steve Detoni)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Fastest way to index thru an array????
  5. Date: 16 Jan 1996 12:23:48 +1300
  6. Message-ID: <4denm4$514@hn.ocbbs.gen.nz>
  7. References: <4d1g3k$o09@solaris.cc.vt.edu>
  8. NNTP-Posting-Host: hn.hn.planet.gen.nz
  9. X-Newsreader: TIN [version 1.2 PL2]
  10.  
  11. Ashutosh Gokhale (ashutosh) wrote:
  12. : Suppose I have an array A[][][] which I declare as
  13. :     double ***A and then assign memory to it using new operator.
  14. : Now consider that A has dimensions A[50][60][70]. Then I can index thru ALL
  15. : entries of A using
  16. : for(i=1; i<50; i++)
  17. :  {
  18. :    for(j=1; j<60; j++)
  19. :     {
  20. :       for(k=1; k<70; k++)
  21. :        {
  22. :          A[i][j][k] = <some expression>;
  23. :        }
  24. :     }
  25. :  }
  26. : But the above way is surely the most time-INEFFICIENT way.
  27. : Can someone suggest me the MOST time efficient way of indexing through A[][][]?
  28.  
  29. : Thanks a lot
  30. Umm not really .... you can treat the whole array as a single dimention 
  31. if you so desire .... so :
  32. const int Z = 50, Y = 60, X = 70;
  33. const int MAXITS = Z * (X * Y); 
  34. int  A[Z][Y][X];
  35. int* pA = A;
  36.  
  37. for (long int count = 0; count < MAXITS; count++)
  38.     pA[count] = 0; 
  39.  
  40. // The above should work if you want to initialise all data items to 0,
  41. // ore you can use memset(), 
  42. memset (A, sizeof (A), 0); // I think from memory.
  43.  
  44. // or even
  45. for (long int count = 0; count < MAXITS; count++)
  46.     A[count] = 0; // No array size checking !!!
  47.  
  48. etc
  49. If you are trying to find an item within a array then some sort of sort 
  50. algorithm may be need to be implemented, something like quick sort, heap 
  51. sort that has the corrosponding Numeric value and indexs.
  52.  
  53. Steve.
  54.  
  55.